package api

import (
	"context"
	"gomain/app/model"
	"gomain/app/service"

	"github.com/gogf/gf/v2/frame/g"
)

type ProductApi struct{}

type ProductListReq struct {
	g.Meta `path:"/product/list" method:"get"`
	Page   int `v:"required#请输入页码"`
	Limit  int `v:"required#请输入每页数量"`
}

type ProductListRes struct {
	List  []model.Product `json:"list"`
	Total int             `json:"total"`
}

func (a *ProductApi) List(ctx context.Context, req *ProductListReq) (res *ProductListRes, err error) {
	products, count, err := service.Product.List(req.Page, req.Limit)
	if err != nil {
		return nil, err
	}
	return &ProductListRes{
		List:  products,
		Total: count,
	}, nil
}

type ProductAddReq struct {
	g.Meta `path:"/product/add" method:"post"`
	model.Product
}

type ProductAddRes struct{}

func (a *ProductApi) Add(ctx context.Context, req *ProductAddReq) (res *ProductAddRes, err error) {
	err = service.Product.Add(&req.Product)
	return
}

type ProductUpdateReq struct {
	g.Meta `path:"/product/update" method:"post"`
	model.Product
}

type ProductUpdateRes struct{}

func (a *ProductApi) Update(ctx context.Context, req *ProductUpdateReq) (res *ProductUpdateRes, err error) {
	err = service.Product.Update(&req.Product)
	return
}

type ProductDeleteReq struct {
	g.Meta `path:"/product/delete" method:"delete"`
	Id     int `v:"required#请选择要删除的商品"`
}

type ProductDeleteRes struct{}

func (a *ProductApi) Delete(ctx context.Context, req *ProductDeleteReq) (res *ProductDeleteRes, err error) {
	err = service.Product.Delete(req.Id)
	return
}
